﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using TradeForm.Properties;
using System.Configuration;
using TradeForm.config;
using System.Xml.Linq;
using System.Xml;

namespace TradeForm.Print
{
    public class CustomBillDocument : DocumentBase
    {
        //private string _defCode = string.Empty;
        //private BillMain _bill = null;
        //private List<Goods> _lstGoods = null;
        //private CompanyInfo _compny = null;
        //private int _goodsItemNo = 0;
        //Dictionary<string, Setting> dicPrintSets = new Dictionary<string, Setting>();

        //放置配置的字典
        protected Dictionary<string, Setting> dicPrintSets = new Dictionary<string, Setting>();

        public CustomBillDocument()
        { }
        public CustomBillDocument(string defcode)
        {
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();

        }

        public CustomBillDocument(string defcode, bool isPrintBack)
        {
            this.IsPrintBack = isPrintBack;
            this._defCode = defcode;
            getData(defcode);
            loadPrintSettings();
        }



        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            g.PageUnit = GraphicsUnit.Millimeter;
            float fontHeight = g.MeasureString("字体测试高度", base.Font).Height;
            if (this.IsPrintBack)
            {
                g.DrawImage(Image.FromFile(@"BackImage\B1.jpg"), 0, 0, Convert.ToSingle(e.PageBounds.Width * 25.4 / 100),
                    Convert.ToSingle(e.PageBounds.Height * 25.4 / 100));
            }
            if (this.cust != null && this.goodsDetails != null && this.goodsDetails.Count > 0 && this.goodsSum != null)
            {


                // g.DrawString(cust.CustomPreNo, base.Font, Brushes.Black, dicPrintSets["CustomPreNo"].PosX, dicPrintSets["CustomPreNo"].PosY);
                //  g.DrawString(cust.CustomNo, base.Font, Brushes.Black, dicPrintSets["CustomNo"].PosX, dicPrintSets["CustomNo"].PosY);

                g.DrawString(cust.FromPort, base.Font, Brushes.Black, dicPrintSets["OutPort"].PosX, dicPrintSets["OutPort"].PosY);
                g.DrawString(cust.RecordNo, base.Font, Brushes.Black, dicPrintSets["RecordNo"].PosX, dicPrintSets["RecordNo"].PosY);
                g.DrawString(cust.ExpDate.Value.ToString("yyyy年MM月dd日"), base.Font, Brushes.Black, dicPrintSets["ExpDate"].PosX, dicPrintSets["ExpDate"].PosY);
                g.DrawString(cust.CustomDate.Value.ToString("yyyy年MM月dd日"), base.Font, Brushes.Black, dicPrintSets["CustomDate"].PosX, dicPrintSets["CustomDate"].PosY);


                g.DrawString(cust.OperateCompany, base.Font, Brushes.Black, dicPrintSets["OperateCompany"].PosX, dicPrintSets["OperateCompany"].PosY);
                g.DrawString(cust.TransportMode, base.Font, Brushes.Black, dicPrintSets["TransportMode"].PosX, dicPrintSets["TransportMode"].PosY);
                g.DrawString(cust.Conveyance, base.Font, Brushes.Black, dicPrintSets["Conveyance"].PosX, dicPrintSets["Conveyance"].PosY);
                g.DrawString(cust.BillLadingNo, base.Font, Brushes.Black, dicPrintSets["BillLadingNo"].PosX, dicPrintSets["BillLadingNo"].PosY);

                g.DrawString(cust.Consignor, base.Font, Brushes.Black, dicPrintSets["Consignor"].PosX, dicPrintSets["Consignor"].PosY);
                g.DrawString(cust.TradeKind, base.Font, Brushes.Black, dicPrintSets["TradeKind"].PosX, dicPrintSets["TradeKind"].PosY);
                g.DrawString(cust.TaxDutyFree, base.Font, Brushes.Black, dicPrintSets["TaxDutyFree"].PosX, dicPrintSets["TaxDutyFree"].PosY);
                g.DrawString(cust.Remittanceway, base.Font, Brushes.Black, dicPrintSets["RemittanceWay"].PosX, dicPrintSets["RemittanceWay"].PosY);

                g.DrawString(cust.WarrantNo, base.Font, Brushes.Black, dicPrintSets["WarrantNo"].PosX, dicPrintSets["WarrantNo"].PosY);
                g.DrawString(cust.TradeCountry, base.Font, Brushes.Black, dicPrintSets["TradeCountry"].PosX, dicPrintSets["TradeCountry"].PosY);
                g.DrawString(cust.DestPort, base.Font, Brushes.Black, dicPrintSets["DestPort"].PosX, dicPrintSets["DestPort"].PosY);
                g.DrawString(cust.GoodsFrom, base.Font, Brushes.Black, dicPrintSets["GoodsFrom"].PosX, dicPrintSets["GoodsFrom"].PosY);

                g.DrawString(cust.ApprovalNo, base.Font, Brushes.Black, dicPrintSets["ApprovalNo"].PosX, dicPrintSets["ApprovalNo"].PosY);
                g.DrawString(cust.TurnoverWay, base.Font, Brushes.Black, dicPrintSets["TurnoverWay"].PosX, dicPrintSets["TurnoverWay"].PosY);
                g.DrawString(cust.Freight, base.Font, Brushes.Black, dicPrintSets["FreightPay"].PosX, dicPrintSets["FreightPay"].PosY);
                g.DrawString(cust.Insurance, base.Font, Brushes.Black, dicPrintSets["Insurance"].PosX, dicPrintSets["Insurance"].PosY);
                g.DrawString(cust.MiscellaneousFees, base.Font, Brushes.Black, dicPrintSets["MiscellaneousFees"].PosX, dicPrintSets["MiscellaneousFees"].PosY);

                g.DrawString(cust.ContractNo, base.Font, Brushes.Black, dicPrintSets["ContractNo"].PosX, dicPrintSets["ContractNo"].PosY);
                g.DrawString(cust.TotalCount.ToString(), base.Font, Brushes.Black, dicPrintSets["TotalCount"].PosX, dicPrintSets["TotalCount"].PosY);
                g.DrawString(cust.PackType, base.Font, Brushes.Black, dicPrintSets["PackType"].PosX, dicPrintSets["PackType"].PosY);
                g.DrawString(cust.TotalGrossWeight.ToString(), base.Font, Brushes.Black, dicPrintSets["TotalGrossWeight"].PosX, dicPrintSets["TotalGrossWeight"].PosY);
                g.DrawString(cust.TotalNetWeight.ToString(), base.Font, Brushes.Black, dicPrintSets["TotalNetWeight"].PosX, dicPrintSets["TotalNetWeight"].PosY);

                g.DrawString(cust.ContainnerNO, base.Font, Brushes.Black, dicPrintSets["ContainnerNO"].PosX, dicPrintSets["ContainnerNO"].PosY);
                g.DrawString(cust.AttachFile, base.Font, Brushes.Black, dicPrintSets["AttachFile"].PosX, dicPrintSets["AttachFile"].PosY);
                g.DrawString(cust.Manufacture, base.Font, Brushes.Black, dicPrintSets["Manufacture"].PosX, dicPrintSets["Manufacture"].PosY);

                g.DrawString(cust.Mark, base.Font, Brushes.Black, dicPrintSets["Mark"].PosX, dicPrintSets["Mark"].PosY);

                int i = 0;
                float goodspan = 12.0f;
                foreach (GoodsDetail item in this.goodsDetails)
                {
                    g.DrawString(item.ItemNo.ToString(), base.Font, Brushes.Black, dicPrintSets["Goods"].PosX,
                        dicPrintSets["Goods"].PosY + i * goodspan);
                    g.DrawString(item.HSCode, base.Font, Brushes.Black, dicPrintSets["Goods"].PosX + 10,
                        dicPrintSets["Goods"].PosY + i * goodspan);
                    g.DrawString(item.GoodsName, base.Font, Brushes.Black, dicPrintSets["Goods"].PosX + 30,
                        dicPrintSets["Goods"].PosY + i * goodspan);
                    g.DrawString(item.Qty.ToString() + item.QtyUnit, base.Font, Brushes.Black, dicPrintSets["Goods"].PosX + 75,
                        dicPrintSets["Goods"].PosY + i * goodspan);
                    //g.DrawString(cust.TradeCountry, base.Font, Brushes.Black, dicPrintSets["Goods"].PosX + 100,
                       // dicPrintSets["Goods"].PosY + i * goodspan);

                    g.DrawString(item.UnitPrice.Value.ToString(), base.Font, Brushes.Black, dicPrintSets["Goods"].PosX + 120,
                        dicPrintSets["Goods"].PosY + i * goodspan);
                    g.DrawString(item.Amount.Value.ToString(), base.Font, Brushes.Black, dicPrintSets["Goods"].PosX + 145,
                        dicPrintSets["Goods"].PosY + i * goodspan);
                    g.DrawString(item.Currency, base.Font, Brushes.Black, dicPrintSets["Goods"].PosX + 165,
                        dicPrintSets["Goods"].PosY + i * goodspan);
                    i++;
                }

                g.DrawString(cust.Tax, base.Font, Brushes.Black, dicPrintSets["Tax"].PosX, dicPrintSets["Tax"].PosY);

                g.DrawString(cust.FillDate.Value.ToString("yyyy年MM月dd日"), base.Font, Brushes.Black, dicPrintSets["TypeDate"].PosX, dicPrintSets["TypeDate"].PosY);
            }
            if (this._compny != null)
            {

                g.DrawString(_compny.DeclareCompany, base.Font, Brushes.Black, dicPrintSets["DeclarationCompany"].PosX, dicPrintSets["DeclarationCompany"].PosY);
                g.DrawString(_compny.CAddress, base.Font, Brushes.Black, dicPrintSets["Address"].PosX, dicPrintSets["Address"].PosY);
                g.DrawString(_compny.Tel, base.Font, Brushes.Black, dicPrintSets["Tel"].PosX, dicPrintSets["Tel"].PosY);
                g.DrawString(_compny.PostCode, base.Font, Brushes.Black, dicPrintSets["PostCode"].PosX, dicPrintSets["PostCode"].PosY);
                g.DrawString(_compny.Typer, base.Font, Brushes.Black, dicPrintSets["Typer"].PosX, dicPrintSets["Typer"].PosY);
                g.DrawString(_compny.TypeCompany, base.Font, Brushes.Black, dicPrintSets["TypeCompany"].PosX, dicPrintSets["TypeCompany"].PosY);
                g.DrawString(_compny.MembersDeclaration, base.Font, Brushes.Black, dicPrintSets["MembersDeclaration"].PosX, dicPrintSets["MembersDeclaration"].PosY);

            }
        }

        //private void getData(string defcode)
        //{
        //    DataDBDataContext dbcntxt = new DataDBDataContext
        //            (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);

        //    var items = from billmain in dbcntxt.BillMain
        //                where billmain.DefCode == defcode
        //                select billmain;
        //    if (items != null && items.Count() > 0)
        //    {
        //        this._bill = items.First();
        //    }
        //    var goods = from good in dbcntxt.Goods
        //                where good.DefCode == defcode
        //                select good;
        //    if (goods != null)
        //    {
        //        this._lstGoods = goods.ToList();
        //    }
        //    var coms = from com in dbcntxt.CompanyInfo
        //               select com;
        //    if (coms != null && coms.Count() > 0)
        //    {
        //        this._compny = coms.First();
        //    }
        //}
        protected void loadPrintSettings()
        {
            XElement printSettings = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + @"\config\CustomBillSettings.xml");
            IEnumerable<XElement> settings = null;
            settings = (from el in printSettings.Elements("setting")
                        select el);
            if (settings != null && settings.Count() > 0)
            {
                foreach (XElement item in settings)
                {
                    if (!dicPrintSets.ContainsKey(item.Attribute("name").Value))
                    {

                        Setting set = new Setting();
                        set.Name = item.Attribute("name").Value;
                        set.CName = item.Attribute("cname").Value;
                        set.PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        set.PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                        dicPrintSets.Add(set.Name, set);
                    }
                    else
                    {
                        dicPrintSets[item.Attribute("name").Value].CName = item.Attribute("cname").Value;
                        dicPrintSets[item.Attribute("name").Value].PosX = Convert.ToSingle(item.Attribute("PosX").Value);
                        dicPrintSets[item.Attribute("name").Value].PosY = Convert.ToSingle(item.Attribute("PosY").Value);
                    }
                }

            }
            else
            {

            }

        }
    }
}
